﻿using System;
using System.Data.Entity;
using System.Data.Objects;
using System.Linq;
using Wamm.AbstractDataAccess;
using Wamm.Model.Abstract;

namespace Wamm.EntityFrameworkImplementation {
    public class Repository<T> : IRepository<T>
                                        where T : class, IEntity {
        private readonly WammCatalog _wammCatalog;
        private readonly IDbSet<T> _dbSet;

        public Repository(WammCatalog wammCatalog)
        {
            _wammCatalog = wammCatalog;
            _dbSet = _wammCatalog.Set<T>();
        }

        public IQueryable<T> FindAll() {
            return _dbSet;
        }

        public IQueryable<T> FindWhere(System.Linq.Expressions.Expression<Func<T, bool>> predicate) {
            return _dbSet.Where(predicate);
        }

        public T FindById(int id) {
            return _dbSet.SingleOrDefault(o => o.Id == id);
        }

        public void Add(T entity) {
            _dbSet.Add(entity);
        }

        public void Remove(T entity) {
            _dbSet.Remove(entity);
        }

        public void RemoveById(int id) {
            Remove(FindById(id));
        }
    }
}
